import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class CountDuplicates {

	public static void main(String[] args) throws IOException {
	    FileReader fileReader = null;
	    BufferedReader bufferReader =null;

	    int countLine = 1;
	    String line = "";
	    
	    Scanner scanner = new Scanner(System.in);
	    System.out.print("Enter a file name: ");
	    System.out.flush();
	    String filename = scanner.nextLine();
	    File file = new File(filename);
	    
	    /*test line w/o input 
	    f_reader = new FileReader("src/input.txt"); */
	    
	    fileReader = new FileReader(file);
	    bufferReader = new BufferedReader(fileReader);
	    
	    System.out.println("Printing words by occurrence per line.");
	    
	    while((line = bufferReader.readLine()) != null){
	        System.out.print("Line " +countLine+ " words: ");
	        String sanatized_line = line.replaceAll("\\p{Punct}|\\d", "").toLowerCase();
	    	findDuplicates(sanatized_line);
	    	System.out.println();
	    	countLine++;
	    }
	    fileReader.close();
	    bufferReader.close();
	    scanner.close();
	}
	
  public static void findDuplicates (String line) {
    List<String> list = Arrays.asList(line.split(" "));
        
    Set<String> uniqueWords = new HashSet<String>(list);
    for (String word : uniqueWords) {
      System.out.print("" +word + ": " + Collections.frequency(list, word)+ " ");
    }
  }
}